package com.amadeus.commons.log.dao;

import com.amadeus.commons.log.entity.ErrorInstance;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;

public class MongoErrorInstanceDAO implements ErrorInstanceDAO {

	private static MongoErrorInstanceDAO _instance;
	private final DBCollection coll;

	private MongoErrorInstanceDAO(Mongo m) {
		DB db = m.getDB("logexplorer");
		coll = db.getCollection("errors");
	}

	public static synchronized ErrorInstanceDAO getInstance(Mongo m) {
		if (_instance == null) {
			_instance = new MongoErrorInstanceDAO(m);
		}
		return _instance;
	}

	public void insert(ErrorInstance errorInstance) {

		BasicDBObject doc = new BasicDBObject();

		doc.put("jsessionid", errorInstance.getJsi());
		doc.put("timestamp", errorInstance.getDate());
		doc.put("errormessage", errorInstance.getErrorMessage());

		coll.insert(doc);

	}

}
